package com.lj.ljminsu.controller;

import com.lj.ljminsu.bean.JsonResultVo;
import com.lj.ljminsu.service.IUserDaoService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("user")
@RestController
public class ForgetPwdController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private IUserDaoService iUserDaoService;

    @CrossOrigin
    @RequestMapping("resetPwdForm")
    public JsonResultVo toForgetFwd(String userPhone,String verifyCodePic,String newPassword,String verifyCode) {
        JsonResultVo jsonResultVo = new JsonResultVo();
        String text = stringRedisTemplate.opsForValue().get("text");
        String existPhone = iUserDaoService.checkPhone(userPhone);
        if (!verifyCode.equalsIgnoreCase(text)) {
            jsonResultVo.setCode(3);
            jsonResultVo.setMsg("图形验证码错误");
            return jsonResultVo;
        }
        if (existPhone==null||"".equals(existPhone)) {
            jsonResultVo.setCode(1);
            jsonResultVo.setMsg("您的号码未注册，请注册");
            return jsonResultVo;
        }
        String s = stringRedisTemplate.opsForValue().get(userPhone + "phoneSecurityCode");
        if (verifyCodePic.equals(s)) {
            iUserDaoService.updatePasswordByUserPhone(newPassword,userPhone);
            jsonResultVo.setCode(0);
            jsonResultVo.setMsg("密码修改成功");
            return jsonResultVo;
        }else {
            jsonResultVo.setCode(2);
            jsonResultVo.setMsg("手机验证码错误");
            return jsonResultVo;
        }

    }
}
